layui.use(['form','jquery','layer','table'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;

    //收起按钮
    $('#advancedSearch').click(function () {
        if($(this).text() == ' 收起'){
            $('.layui-card-body').slideUp(300,'linear');
            $('#advancedSearch').text(' 展示').removeClass('layui-icon-down').addClass('layui-icon-up');
        }else if($(this).text() == ' 展示'){
            $('.layui-card-body').slideDown(300,'linear');
            $('#advancedSearch').text(' 收起').removeClass('layui-icon-up').addClass('layui-icon-down');
        }
    })


    getCourseOrdersDoneStatusSourcePay();
    //根据订单状态、订单来源、订单支付渠道查询订单
    form.on('submit(search)',function (obj) {
        getCourseOrdersDoneStatusSourcePay(obj.field);
        return false;
    });
    function getCourseOrdersDoneStatusSourcePay(StatusSourcePay) {
        $.ajax({
            url: '/SyUserOrder/getCourseOrdersDoneStatusSourcePay',
            data: StatusSourcePay,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                ordersRender(data.data.data);
            }
        });
    }
    //将查出来的数据渲染到表格中
    function ordersRender(data) {
        table.render({
            elem: '#ordersTable',
            title: '审核列表',
            data: data,
            limit: 9,
            width: 1260,
            size: 'lg',
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',fixed:'left'},
                {title:'序号',align:'center',width:60,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"orderLogisticsNumber",title:"订单号",align:'center',width:140},
                {field:"beforeAccount",title:"用户昵称",align:'center',width:100},
                {field:"beforePhone",title:"用户手机",align:'center',width:150},
                {field:"orderStatus",title:"订单状态",align:'center',width:100,templet:function(data){
                        if(data.orderStatus == 2){
                            return '<p>未付款</p>';
                        }else if(data.orderStatus == 3){
                            return '<p>已付款</p>';
                        }else if(data.orderStatus == 6){
                            return '<p>已签收</p>';
                        }else if(data.orderStatus == 7){
                            return '<p>退货申请</p>';
                        }else if(data.orderStatus == 8){
                            return '<p>退货中</p>';
                        }else if(data.orderStatus == 9){
                            return '<p>已退货</p>';
                        }else if(data.orderStatus == 10){
                            return '<p>取消交易</p>';
                        }
                    }},
                {field:"courseName",title:"课程名称",align:'center',width:150},
                {field:"roleName",title:"主讲教师",align:'center',width:150},
                {field:'',title:"操作",align:'center',width:175,toolbar: '#operation'}
            ]]
        });
    }
    table.on('tool(ordersTable)',function (data) {
        //查看订单表详情信息
        if (data.event == 'selectTable') {
            layer.open({
                type: 2,
                title: '订单详情',
                content: '/courseordermanagement/courseorderlist/order-information.html',
                area: ['1150px', '550px'],
                offset: ['10px', '50px'],
                shade: 0.4,
                btn:['确定'],
                yes:function(index,layero){
                    layer.close(index);
                },
                success:function(layero,index) {
                    getProductByOrderId(data.data.orderId,layero);
                }

            })
            //删除按钮
        }else if(data.event == 'deleteTable'){
            delCurrentOrder(data.data.orderId);
        }
    })
    //删除当前行订单
    function delCurrentOrder(orderId) {
        $.ajax({
            url: '/SyUserOrder/deleteCurrentOrder',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                layer.msg(data.data);
                getCourseOrdersDoneStatusSourcePay();
            }
        });
    }

    //查看当前订单有关信息
    function getProductByOrderId(orderId,layero) {
        $.ajax({
            url: '/heat/getCourseByOrderId',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                var course = data.data;
                var childHtml = layero.find('iframe').contents();
                childHtml.find('#courseName').text(course.courseName);
                childHtml.find('#name').text(course.name);
                childHtml.find('#courseNprice').text(course.courseNprice);
                childHtml.find('#collectNum').text(course.collectNum);
                var courseStatu;
                if(course.courseStatus == 2){
                    courseStatu = '上架'
                }else if(course.courseStatus == 3){
                    courseStatu = '未上架'
                }
                childHtml.find('#courseStatus').text(courseStatu);
                childHtml.find('#likeNum').text(course.likeNum);
                childHtml.find('#saleNum').text(course.saleNum);
                childHtml.find('#courseTime').text(course.courseTime + '分钟');
                childHtml.find('#courseIntro').text(course.courseIntro);
                childHtml.find('#courseInfo').text(course.courseInfo);
                childHtml.find('#courseUrl').attr('src',course.courseUrl);
            }
        });
    }
})